const app = getApp()
import {
  ajaxGetUserGameRecordsOutVoList
} from '../../../api/my'
Page({
  data: {
    //自定义导航高度
    customNavHeight: app.globalData.customNavInfo.navHeight,
    baseURL: app.globalData.config.baseURL,
    k1UserId: '',
    bestTime: '',
    dataList:[],
    cloneList:[],

    option: [
      { text: '默认', value: 0 },
      { text: '时间升序', value: 1 },
      { text: '时间降序', value: 2 },
      { text: '最佳圈数', value: 3 },
    ],
    value: 0,
    screenDate: ['',''], // 筛选的时间

  },
  onLoad(options) {

    const { k1UserId, bestTime } = options
    ajaxGetUserGameRecordsOutVoList({
      k1UserId,
      // k1UserId: 200065
    }).then(({data})=>{
      data = (data||[]).map(item=>{
        console.log(item);
        item.$id = item.sessionId
        item.bestTime || (item.bestTime = 9999999)
        return item
      })
      this.setData({
        k1UserId: k1UserId,
        bestTime: bestTime,
        dataList: data||[],
        cloneList: JSON.parse(JSON.stringify(data||[]))
      })
    })
  },
  toRankingDetail({ currentTarget }){
    const { id } = currentTarget.dataset
    const { k1UserId } = this.data
    app.commonNavigateTo(`/pages/myInner/rankingDtails/index?k1UserId=${k1UserId}&id=${id}`)
  },
  onSwitchChange(e){
    const map = {
      '0': 'valueDefault',
      '1': 'valueDatePositive',
      '2': 'valueDateReverse',
      '3': 'bestTimeSort',
    }
    this[map[e.detail]]()
  },
  // 默认，什么都不做
  valueDefault(){
    this.setData({
      cloneList: JSON.parse(JSON.stringify(this.data.dataList))
    })
  },
  // 时间正序
  valueDatePositive(){
    const list = this.getScreenDate()
    list.sort((a,b)=>{
      const aDate = a.sessionDate.replace(/\-/g, '/')
      const bDate = b.sessionDate.replace(/\-/g, '/')
      return new Date(bDate).getTime()-new Date(aDate).getTime()
    })
    this.setData({
      cloneList: list
    })
  },
  // 时间倒序
  valueDateReverse(){
    const list = this.getScreenDate()
    list.sort((a,b)=>{
      const aDate = a.sessionDate.replace(/\-/g, '/')
      const bDate = b.sessionDate.replace(/\-/g, '/')
      return new Date(aDate).getTime()-new Date(bDate).getTime()
    })
    this.setData({
      cloneList: list
    })
  },
  // 最佳圈数
  bestTimeSort(){
    const list = this.getScreenDate()
    list.sort((a,b)=>{
      return a.bestTime-b.bestTime
    })
    this.setData({
      cloneList: list
    })
  },
  getScreenDate(){
    if(this.data.screenDate[0]===''||this.data.screenDate[1]===''){
      return JSON.parse(JSON.stringify(this.data.dataList))
    }else {
      const timestamp = this.data.screenDate.map(item=>{
        return new Date(item).getTime()
      })
      return this.data.dataList.filter(item=>{
        const sessionDate = item.sessionDate.replace(/\-/g, '/')
        const time = new Date(sessionDate).getTime()
        return time>timestamp[0] && time<(timestamp[1]+86400000)
      })
    }
  },
  // 选择开始-结束时间
  onCalendarConfirm(e){
    const date = app.dateFormat(e.detail.value, 'Y/m/d')
    const { index } = e.currentTarget.dataset
    this.data.screenDate[index] = date
    this.setData({
      screenDate: this.data.screenDate
    })
    if(index==='1'){
      if(this.data.screenDate[0]!==''){
        const list = this.getScreenDate()
        console.log(list);
        this.setData({
          cloneList: list,
          value: 0,
        })
      }
    }
  },
  // 清空筛选
  empty(){
    this.setData({
      value: 0,
      screenDate: ['', ''], // 筛选的时间
      cloneList: JSON.parse(JSON.stringify(this.data.dataList)),
    })
  },

  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },
})